#include <bits/stdc++.h>
#include <stdlib.h>
#include <stdbool.h>
using namespace std;
#define QDATATYPE int
typedef struct QueueNode
{
    struct QueueNode *next;
    QDATATYPE data;
} QNode;
typedef struct Queue
{
    QNode *head;
    QNode *tail;
    int size;
} Que;
void InitQueue_L(Que *pq)
{
    assert(pq);
    pq->head = pq->tail = NULL;
    pq->size = 0;
}
void QDestroy(Que *pq)
{
    assert(pq);
    QNode *cur = pq->head;
    while (cur)
    {
        QNode *next = cur->next;
        free(cur);
        cur = next;
    }
    pq->head = pq->tail = NULL;
    pq->size = 0;
}
void EnQueue_L(Que *pq, QDATATYPE x)
{
    assert(pq);
    QNode *newnode = (QNode *)malloc(sizeof(QNode));
    newnode->data = x;
    newnode->next = NULL;
    if (newnode == NULL)
    {
        perror("malloc");
        return;
    }
    // 判断一下是否为空，因为没有哨兵位的头节点
    if (pq->tail == NULL)
    {
        assert(pq->head == NULL);
        pq->head = pq->tail = newnode;
    }
    else
    {
        pq->tail->next = newnode;
        pq->tail = newnode;
    }
    pq->size++;
}
// 队只能队尾进队头出
void DeQueue_L(Que *pq,int &n)
{
    assert(pq);
    assert(pq->head);
    n = pq->head->data;
    // 考虑如果head == tail == NULL。
    if (pq->head->next == NULL)
    {
        free(pq->head);
        pq->head = pq->tail = NULL;
    }
    else
    {
        QNode *cur = pq->head->next;
        free(pq->head);
        pq->head = cur;
    }
    pq->size--;
}
int QSize(Que *pq)
{
    assert(pq);
    return pq->size;
}
QDATATYPE QFront(Que *pq)
{
    assert(pq);
    assert(QSize(pq));
    return pq->head->data;
}
void SeeDoctor()
{        
    Que Q;           // 模拟病人在医院等候就诊的过程
    InitQueue_L(&Q); // 初始化链队列Q
    int flag = 1,n;       // flag=1：接收病人；=0：停止就诊
    char ch;
    while(flag)
    {
        cout << "请输入命令：";
        cin >> ch;
        ch = toupper(ch);
        switch (ch)
        {
        case 'A':
            cout << "病历号：";
            cin >> n;
            EnQueue_L(&Q, n); // 入队等候就诊
            break;
        case 'N':
            if (QSize(&Q))
            {
                DeQueue_L(&Q, n); // 病人出队就诊
                cout << "病历号为" << n << "的病人就诊" << endl;
            }
            else
                cout << "无病人等候就诊。" << endl;
            break;
        case 'S':
            cout << "下列排队的病人依次就诊：";
            while (QSize(&Q))
            { 
                // 所有病人依次出队就诊
                DeQueue_L(&Q, n);
                cout << n << " ";
            }
            cout << endl
                 << "今天不再接收病人排队！" << endl;
            flag = 0;
            break;
        default:
            cout << " 输入命令不合法！" << endl;
        }
    }
    QDestroy(&Q);
} // SeeDoctor
int main()
{
    SeeDoctor();
    return 0;
}